









<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
    <title>JUnice - Mock framework</title>
    <link rel="stylesheet" href="./css/maven-theme.css" type="text/css" />
    <link rel="stylesheet" href="./css/maven-base.css" type="text/css"/>
    <link rel="stylesheet" href="./css/prettify.css" type="text/css"/>
    <link rel="stylesheet" href="./css/print.css" type="text/css" media="print" />
    <script type="text/javascript" src="./js/prettify.js"></script>
    <script type="text/javascript" src="./js/niftycube.js"></script>
    <script type="text/javascript">
        window.onload=function() {
            Nifty("div\#wrapper", "big");
            Nifty("div#breadcrumbs", "small");
            Nifty("div\#navcolumn", "small");
            Nifty("div.source", "medium");
            prettyPrint();
        }
    </script>
          <meta name="author" content="Marco Speranza and Simone Tripodi" />
          </head>
  <body>
    <div id="wrapper">
        <div id="banner">
                        <div id="bannerLeft">
    
            JUnice
    
            </div>
                            <div class="clear">
            <hr/>
          </div>
        </div>
        <div id="breadcrumbs">
              
  

  
    
            
  
    
            <div class="xleft">
        <span id="publishDate">Last Published: 2010-01-13</span>
                  &nbsp;| <span id="projectVersion">Version: 1.0-SNAPSHOT</span>
                      </div>
                <div class="xright">      
  

  
    
            
  
    
  </div>
          <div class="clear">
            <hr/>
          </div>
        </div>
        <div id="leftColumn">
          <div id="navcolumn">
               
  

  
    
            
  
    
                       <h5>JUnice</h5>
            <ul>
              
    <li class="none">
                    <a href="index.html">Home</a>
          </li>
              
    <li class="none">
                    <a href="core.html">The core - JUniceRunner</a>
          </li>
              
    <li class="none">
                    <a href="guice.html">Guice integration</a>
          </li>
              
    <li class="none">
              <strong>Mock Framework integration</strong>
        </li>
              
    <li class="none">
                    <a href="JUnice-1.0-SNAPSHOT.pdf">PDF User Manual</a>
          </li>
          </ul>
              <h5>Project Documentation</h5>
            <ul>
              
                
                    
                  
                  
                  
                  
                  
                  
                  
                  
                  
              
        <li class="collapsed">
                    <a href="project-info.html">Project Information</a>
                </li>
              
                
                    
                  
                  
                  
                  
                  
                  
                  
                  
                  
                  
                  
              
        <li class="collapsed">
                    <a href="project-reports.html">Project Reports</a>
                </li>
          </ul>
              <h5>External Links</h5>
            <ul>
              
    <li class="none">
                    <a href="">Project Page</a>
          </li>
              
    <li class="none">
                    <a href="../google-guice/">Google Guice</a>
          </li>
              
    <li class="none">
                    <a href="http://www.junit.org" class="externalLink">JUnit</a>
          </li>
              
    <li class="none">
                    <a href="http://easymock.org/" class="externalLink">Easy-Mock</a>
          </li>
              
    <li class="none">
                    <a href="http://mockito.org/" class="externalLink">Mockito</a>
          </li>
          </ul>
                                               <a href="http://maven.apache.org/" title="Built by Maven" class="poweredBy">
            <img class="poweredBy"
                 alt="Built by Maven"
                 src="./images/logos/maven-feather.png"
                 width="90"
                 height="30"
                 border="1" />
          </a>
                           
  

  
    
            
  
    
            </div>
        </div>
        <div id="bodyColumn">
          <div id="contentBox">
            <div class="section"><h2>Mock framework</h2>
<p><i>JUnice</i> integrates two <i>Mock frameworks</i> integrated with <i>Google Guice</i> to inject the mocked object into bussines logic classes, simply by annotating a test fild with <tt>@com.junice.mock.annotation.Mock</tt> annotation. All existing bindins will be replaced with mock object declared into your test class. </p>
<p>JUnice supports this kind of mock:</p>
<ul><li>Easy-Mock (<tt>com.junice.mock.annotation.MockType.EASY_MOCK</tt>)</li>
<li>Mockito (<tt>com.junice.mock.annotation.MockType.MOCKITO</tt>)<p><b>by default</b> JUnice creates Easy-Mock mock objects.</p>
</li>
</ul>
<div class="section"><h3>Choose your favorite mock framework</h3>
<p>To choose a different mock framework that <i>JUnice</i> shuld be used to create your mock object, you should annotate the test classes in this way:</p>
<div class="source"><pre>
@RunWith(JUniceRunner.class)
@MockFramework(type=MockType.MOCKITO)
public class SimpleTest {

}</pre>
</div>
</div>
<div class="section"><h3>Mock annotation</h3>
<p><tt>com.junice.annotation.Mock</tt> is a field annotation used by core <i>Runner</i> to instantiate and inject Mock object into a test case class.</p>
<p><tt>com.junice.annotation.Mock</tt> can be configured with this parameters:</p>
<table class="bodyTable"><tbody><tr class="a"><th align="left">Property</th>
<th align="left">Type</th>
<th align="left">Description</th>
</tr>
<tr class="b"><td align="left"><b>providedBy</b></td>
<td align="left"><tt>java.lang.String</tt></td>
<td align="left">Specifies a <tt>java.lang.String</tt> that indentifies a <b>public static</b> method with return type is equals to the type of the field annotated by this annotation.</td>
</tr>
<tr class="a"><td align="left"><b>providerClass</b></td>
<td align="left"><tt>Class</tt></td>
<td align="left">If present specifies a <tt>Class</tt> that contains the method <tt>providedBy</tt>, by default<tt>providerClass</tt> is setted with filed declared class</td>
</tr>
<tr class="b"><td align="left"><b>reserAfter</b></td>
<td align="left">boolean</td>
<td align="left"><b>true</b> (default) specifies a boolean value to reset this mock object after any test method.</td>
</tr>
<tr class="a"><td align="left"><b>annotateWith</b></td>
<td align="left"><tt>Class</tt></td>
<td align="left">Specifies an annotaion <tt>Class</tt> that will be used in the <tt>Google Guice</tt> binder to execute the literal binding.</td>
</tr>
<tr class="b"><td align="left"><b>type</b></td>
<td align="left"><tt>com.junice.annotation.MockObjType</tt></td>
<td align="left">Specifies the mock type.</td>
</tr>
</tbody>
</table>
<p>In the following example <i>HelloWorld</i> class dependencies will be injected with the mocked class created by the <i>Runner</i>:</p>
<div class="source"><pre>
public class HelloWorld {

    @Inject
    private Service service;

    ...

    public String sayHalloByService() {
        return service.go();
    }

    ...
}</pre>
</div>
<p>Then, in the test class:</p>
<div class="source"><pre>
@RunWith(JUniceRunner.class)
public class SimpleTest {

    //Create and inject a simple EasyMock Strict mock
    @com.junice.annotation.Mock
    private Service service;

    @Inject
    private HelloWorld helloWorld;

    @Test
    public void testMock(){
        //Stub created mock
        EasyMock.expect(service.go()).andReturn(&quot;Ciao&quot;);
        EasyMock.replay(providedMock);

        assertNotNull(service);
        assertEquals(&quot;Ciao&quot;, helloWorld.sayHalloByService());

        EasyMock.verify(service);
    }

}</pre>
</div>
<div class="section"><h4>Mock resetAfter</h4>
<p><b>Note</b> The runner will reset any mock object after each test method. To indicate that a mock has to be resetted manually, fields must to be annotated in this way:</p>
<div class="source"><pre>
    ...

    @Mock(resetAfter=false) 
    private Service service;

    ...</pre>
</div>
</div>
<div class="section"><h4>Mock provider</h4>
<p>The Runner class create a simple mock for each <tt>@Mock</tt> annotated filed. You shuld delegate the mock creation to a method in this way:</p>
<div class="source"><pre>
@RunWith(JUniceRunner.class)
public class SimpleTest {

  @Mock(providedBy=&quot;providerMethod&quot;) 
  private Service service;

  public static Service providerMethod() {
    Service mocked = EasyMock.createNiceMock(Service.class);

    ... 

    return mockedService;
  }

  ...

}</pre>
</div>
<p>It's possible also delegate an external class:</p>
<div class="source"><pre>
public class ServiceMockProvider {

  public static Service providerMethod() {
    Service mocked = EasyMock.createNiceMock(Service.class);

    ... 

    return mockedService;
  }
}</pre>
</div>
<div class="source"><pre>
@RunWith(JUniceRunner.class)
public class SimpleTest {

    @Mock(providedBy=&quot;providerMethod&quot;, 
                      providerClass=ServiceMockProvider.class) 
    private Service service;

    ...
}</pre>
</div>
</div>
<div class="section"><h4>Mock type</h4>
<p>The Runner class create a simple mock for each <tt>@Mock</tt> annotated filed. It's possible specify witch type of mock that the Runner creates. The possible values are:</p>
<ul><li>EASY_MOCK_NORMAL to create a normal EasyMock object mock (default value)</li>
<li>EASY_MOCK_STRICT to create a strict EasyMock object mock</li>
<li>EASY_MOCK_NICE to create a nice EasyMock object mock </li>
</ul>
</div>
</div>
<div class="section"><h3>Replace module</h3>
<p><i>JUnice</i> core <i>Runner</i> will replace all existing bindings found into modules declared via <tt>@com.junice.annotation.GuiceModule</tt> and <tt>@com.junice.annotation.GuiceProvidedModules</tt> with mocked object annotated with <tt>com.junice.annotation.Mock</tt></p>
<p>So given google-guice module:</p>
<div class="source"><pre>
public class ServiceModule extends AbstractModule {

    @Override
    protected void configure() {
        bind(Service.class)
            .to(ServiceImpl.class)
                .asEagerSingleton();
        ...
    }
}</pre>
</div>
<p>and your implementation</p>
<div class="source"><pre>
public class ServiceImpl implements Service {

    public String go() {
        return &quot;It's real class&quot;;
    }

}</pre>
</div>
<div class="source"><pre>  
@RunWith(JUniceRunner.class)
@GuiceModules(modules=ServiceModule.class)
public class TestCustomInjectionTest {

    @Mock
    private static Service service;
    
    @Test
    public void testOverideModule() throws Exception {
        assertNotNull(service);
        
        EasyMock.expect(service.go())
                .andReturn(&quot;Mocked injected class&quot;);
        
        assertEquals(&quot;Mocked injected class&quot;, service.go());
    }
    
}
    </pre>
</div>
<p>service instance will be injected with the mocked object. </p>
</div>
<div class="section"><h3>Multiple annotation for same type</h3>
<p>It's possible annotate multiple types with the <tt>com.junice.annotation.Mock</tt> annotation</p>
<p>The Method <tt>GuiceMockModule#configure()</tt> creates a binding for each <tt>com.junice.annotation.Mock</tt> found. The binding will be created <b>if and only if</b> there is no types conflict beetween declared <tt>com.junice.annotation.Mock</tt>.</p>
<p>A type conflict is detected </p>
<ul><li>if two or more field are annotated with the same <tt>com.junice.annotation.Mock</tt> and no different <tt>com.junice.annotation.Mock#annotatedWith</tt> parameter are specified for these annotation or, </li>
<li>two, or more, equals <tt>com.junice.annotation.Mock#annotatedWith</tt> parameter are specified for the same type field.<p>If a conflict is detected the <b>binding will not be created</b> for this conflicted type, and the field will be injected <b>only</b> into the test class. So if necessary you have to create a proper binding for this <tt>com.junice.annotation.Mock</tt> field:</p>
<div class="source"><pre>  
@RunWith(JUniceRunner.class)
public class TestCustomInjectionTest extendes Module {

    public void configure(Binder binder) {
        binder.bind(Service.class).toInstance(serviceOne);
    }

    @Mock
    private static Service serviceOne;

    @Mock
    private Service serviceTwo;
    
    @Inject
    private HelloWorld helloWorld;
    
    @Test
    public void testOverideModule() throws Exception {
        assertNotNull(serviceOne);
        assertNotNull(serviceTwo);
    }
}</pre>
</div>
</li>
</ul>
<p>in this case <tt>HelloWorld</tt> class has a dependency with <tt>Service</tt> interface. If you dont specify a proper binding <tt>Google Guice</tt> raises an <tt>com.google.inject.ConfigurationExcepion</tt>. </p>
</div>
</div>

          </div>
        </div>
        <div class="clear">
          <hr/>
        </div>
        <div id="footer">
          <div class="xright">Copyright &#169;         
   
          2010.
    
      All Rights Reserved.    
  

  
    
            
  
    
  </div>
          <div class="clear">
            <hr/>
          </div>
        </div>
    </div>
  </body>
</html>
